﻿using LbangCMS.Areas.Admin.Models;
using LbangCMS.EFModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LbangCMS.Areas.Admin.Controllers
{
    [Authorize]
    public class JobController : Controller
    {
        // GET: Admin/Job
        public ActionResult List()
        {
            var model = new List<JobModel>();
            var jobs = GetJobList();
            if(jobs!=null&&jobs.Any())
            {
                foreach (var item in jobs)
                {
                    var m = new JobModel()
                    {
                        ID = item.ID,
                        JobName = item.JobName,
                        Experience = item.Experience,
                        Description = item.Description,
                        Location = item.Location,
                        Quantity = item.Quantity,
                        Status = item.Status==1
                    };
                    model.Add(m);
                }
               
            }
            if(Request.IsAjaxRequest())
            {
                return PartialView("_ListPartial", model);
            }
            else
            {
                return View(model);
            }
           
        }

        public ActionResult Edit(int id=0)
        {
            if(id>0)
            {
                var job = GetJob(id);
                if(job!=null)
                {
                    var model = new JobModel()
                    {
                        ID = job.ID,
                        JobName = job.JobName,
                        Experience = job.Experience,
                        Description = job.Description,
                        Location = job.Location,
                        Quantity = job.Quantity,
                        Status=job.Status==1
                    };
                    return View(model);
                }
            }
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(JobModel model)
        {
            if(ModelState.IsValid)
            {
                if(model.ID>0)
                {
                    UpdateJob(model.ID, model.JobName, model.Location, model.Quantity, model.Experience, model.Status?1:0, model.Description);
                }
                else
                {
                    AddJob(model.JobName, model.Location, model.Quantity, model.Experience, model.Status?1:0, model.Description);
                }
            }
            return Json(new { success=true });
        }

        public ActionResult Remove(int id)
        {
            RemoveJob(id);
            return Json(new { success = true });
        }

        private void AddJob(string jobName,string location,int quantity,string experience,int status,string description)
        {
            using (var db = new Database())
            {
                var job = new Job()
                {
                    JobName = jobName,
                    Location = location,
                    Quantity = quantity,
                    Experience = experience,
                    Status = status,
                    Description = description
                };
                db.Jobs.Add(job);
                db.SaveChanges();
            }
        }

        private void UpdateJob(int id, string jobName, string location, int quantity, string experience, int status, string description)
        {
            using (var db = new Database())
            {
                var job = db.Jobs.Where(o => o.ID == id).FirstOrDefault();
                if(job!=null)
                {
                    job.JobName = jobName;
                    job.Location = location;
                    job.Quantity = quantity;
                    job.Experience = experience;
                    job.Status = status;
                    job.Description = description;
                    db.SaveChanges();
                }
            }
        }

        private void RemoveJob(int id)
        {
            using (var db = new Database())
            {
                var job=db.Jobs.Where(o => o.ID == id).FirstOrDefault();
                db.Jobs.Remove(job);
                db.SaveChanges();
            }
        }

        private Job GetJob(int id)
        {
            using (var db = new Database())
            {
                return db.Jobs.Where(o => o.ID == id).FirstOrDefault();
            }
        }

        private List<Job> GetJobList()
        {
            using (var db = new Database())
            {
                return db.Jobs.ToList();
            }
        }
    }
}